import os
import shutil

template = """            {
                    title: "{ abab }",
                    key: "{ abab }",
                    width: 200,
                    sortable: true,
                    treeNode: true,

                    type: "{ ababtype }",
                    /*search: {
                    component: {
                        props: {
                            clearable: true
                        }
                    }
                    },*/
                    form: {
                        editDisabled: true,
                        rules: [
                            // 表单校验规则
                            { required: true, message: "{ abab }必填" }
                        ],
                        component: {
                            props: {
                                clearable: true
                            },
                            placeholder: "请输入{ abab }"
                        },
                        itemProps: {
                            class: { yxtInput: true }
                        }
                    }
                },
    """

d = {
    "Char": "input",
    "Text": "input",
    "Integer": "number",
    "Decimal": "number",
    "Float": "number",
    "DateTime": "datetime",
    "Date": "date",
}

route_model = [ ("permission",       "EntityGongyexvkeViewSet"),
("iso9000",          "EntityIso9000ViewSet")]

dir = os.listdir("web/src/views/risk/basedata")
for (k,v) in route_model:
    if k not in dir:
        shutil.copytree("web/src/views/risk/basedata/complaints", f"web/src/views/risk/basedata/{k}")

rm_dict = {k[:-7]:v for v,k in route_model}

with open("backend/risk/models.py",'r') as f:
    s = f.read()

classes = [c for c in s.split("\nclass ") if c.split("(")[0] in rm_dict]
for aim in classes:

    class_name = aim.split('(')[0]
    path = rm_dict[class_name]
    print(path)

    lines = aim.split("class")[0].split("\n    ")[1:-1]

    kvs = [l.split(" = models.") for l in lines]

    kvs = [ (l[0],l[1].split("Field")[0]) for l in kvs if l and l[0]]

    with open(f"web/src/views/risk/basedata/{path}/temp.txt", 'w') as f:
        f.write(f"{path}\n\n")
        for i, j in kvs:
            f.write(template.replace('{ abab }',i).replace('{ ababtype }',d[j]))